package quartz.job;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class FirstJobScheduler {
	public static void main(String[] args) throws SchedulerException {
		JobDataMap newJobDataMap=new JobDataMap();
		newJobDataMap.put("hello", "你好");
		JobDataMap newJobDataMap2=new JobDataMap();
		newJobDataMap2.put("hello", "你好啊");
		newJobDataMap2.put("setter", "你好啊setter获取");
		//1、创建JobDetail实例，该实例与FirstJob.class绑定,分组缺失则默认为default
		JobDetail jobDetail = JobBuilder
				.newJob(FirstJob.class)
				.withIdentity("firstJob", "group1")				
				.usingJobData(newJobDataMap)
				.usingJobData("可以.usingJobData传多个值", "基本类型")
				.build();
		System.out.println("jobDetail name:"+jobDetail.getKey().getName());
		System.out.println("jobDetail group:"+jobDetail.getKey().getGroup());
		System.out.println("jobDetail job class:"+jobDetail.getJobClass().getName());
		//2、创建一个Trigger实例，定义该job立即执行，间隔两秒，直到永远。。。
		//此group1分组和上group1分组不是同一个组；各自属于不同实例下的分组
		Trigger trigger=TriggerBuilder.newTrigger()
				.withIdentity("trigger", "group1")
				.usingJobData(newJobDataMap2)//传参
				.startNow()
				.withSchedule(
					SimpleScheduleBuilder.simpleSchedule()
					.withIntervalInSeconds(2)
					.repeatForever()
						)
				.build();
		SchedulerFactory sf=new StdSchedulerFactory();
		Scheduler scheduler=sf.getScheduler();
		scheduler.start();
		//3、传入两个实例执行
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
		System.out.println("start time:"+sdf.format(date));
		scheduler.scheduleJob(jobDetail, trigger);
		
	}
}
